/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_common
 * FILE       : SecureString.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 17, 2010  mycodetalks			       Created
 */
package net.lwf.security;

import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;

/**
 * Class description goes here
 */
public class EncryptDecryptor {
	
	public static final String BASIC_PWD = "basic";
	public static final String STRONG_PWD = "strong";
  
	public String encryptText(String text, String passwordKey) {
		BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
		basicTextEncryptor.setPassword(passwordKey);
		return basicTextEncryptor.encrypt(text);
	}
	
  	public String decryptText(String text, String passwordKey) {
  		BasicTextEncryptor basicTextDecryptor = new BasicTextEncryptor();
		basicTextDecryptor.setPassword(passwordKey);
		return basicTextDecryptor.decrypt(text);
	}
	
  	/*
  	 * http://www.jasypt.org/encrypting-passwords.html
  	 */
	public String encryptPwd(String password, String encryptionType) {
		PasswordEncryptor pwencryptor = null;
		if(encryptionType.equals(BASIC_PWD)) {
			pwencryptor = new BasicPasswordEncryptor();
		}
		else if(encryptionType.equals(STRONG_PWD)) {
			pwencryptor = new StrongPasswordEncryptor();
		}
		else {
			//throw exception
		}
		return pwencryptor.encryptPassword(password);
	}
	
	public boolean isValid(String password, String encryptedPassword, String encryptionType) {
		PasswordEncryptor pwencryptor = null;
		if(encryptionType.equals(BASIC_PWD)) {
			pwencryptor = new BasicPasswordEncryptor();
		}
		else if(encryptionType.equals(STRONG_PWD)) {
			pwencryptor = new StrongPasswordEncryptor();
		}
		else {
			//throw exception
		}
		if(pwencryptor.checkPassword(password, encryptedPassword)) {
			return true;
		}
		return false;
	}

} //SecureString



